<template>
  <div id="app">
    <div class="container">
      <!-- 顶部搜索框模块 -->
      <div class="form-group">
        <div class="input-group">
          <input type="text" class="form-control" placeholder="搜索" />
        </div>
      </div>

      <!-- 数据表格 -->
      <table class="table table-bordered table-hover">
        <thead>
          <tr>
            <th>编号</th>
            <th>资产名称</th>
            <th>价格</th>
            <th>创建时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, ind) in list" :key="item.id">
            <td>{{ item.id }}</td>
            <td>{{ item.name }}</td>
            <td :class="{ red: item.price > 100 }">{{ item.price }}</td>
            <td>{{ item.time | dateFormate }}</td>
            <td><a href="#" @click.prevent="Del(ind)">删除</a></td>
          </tr>
          <tr style="background-color: #eee">
            <td>统计:</td>
            <td colspan="2">总价: {{ allPrice }}¥</td>
            <td colspan="2">平均: {{ svgPrice }}¥</td>
          </tr>
        </tbody>
        <tfoot v-if="list.length == 0">
          <tr>
            <td colspan="4" style="text-align: center">没有什么数据给你啦~~</td>
          </tr>
        </tfoot>
      </table>
      <form class="form-inline">
        <div class="form-group">
          <div class="input-group">
            <input
              v-model.trim="name"
              type="text"
              class="form-control"
              placeholder="资产名称"
            />
          </div>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <div class="form-group">
          <div class="input-group">
            <input
              v-model.trim.number="price"
              type="text"
              class="form-control"
              placeholder="价格"
            />
          </div>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <!-- 因为是form 表单 所以要通过vue 的 .prevent 阻止表单提交 -->
        <button class="btn btn-primary" @click.prevent="hAdd">添加资产</button>
      </form>
    </div>
  </div>
</template>

<script>
//引入moment 模块 需要下载
import moment from "moment";
export default {
  data() {
    return {
      //设置产品名称 和 价格
      name: "",
      price: 0,
      // 本地取出值
      list: JSON.parse(localStorage.getItem("data")) || [],
    };
  },
  filters: {
    dateFormate(val) {
      return moment(val).format("YYYY-MM-DD");
    },
  },
  methods: {
    Del(index) {
      // 删除数组中元素
      this.list.splice(index, 1);
    },
    hAdd() {
      //判断
      if (this.name.length == 0 || this.price.length == 0 || this.price < 0) {
        alert("请写入正确的名字和价格");
        return;
      }

      this.list.push({
        id: this.list.length + 1,
        name: this.name,
        price: this.price,
        time: new Date(),
      });
    },
  },
  watch: {
    list: {
      handler(newArr) {
        //数据存入本地
        localStorage.setItem("data", JSON.stringify(newArr));
      },
      deep: true,
      immediate: true,
    },
  },
  computed: {
    allPrice() {
      return Math.floor(this.list.reduce((sum, obj) => (sum += obj.price), 0));
    },
    svgPrice() {
      return Math.floor(this.allPrice / this.list.length);
    },
  },
};
</script>

<style>
.red {
  color: red;
}
</style>